<script>
  var anchorForId = function (id) {
    var anchor = document.createElement("a");
    anchor.className = "header-link";
    anchor.href      = "#" + id;
    anchor.innerHTML = "<span class=\"sr-only\">Permalink</span><i class=\"fa fa-link\"></i>";
    anchor.title = "Permalink";
    return anchor;
  };

  document.onreadystatechange = function () {
    if (this.readyState === "complete") {
      var className =
        ".show-anchor-links h1, .show-anchor-links h2, .show-anchor-links h3, " +
        ".show-anchor-links h4, .show-anchor-links h5, .show-anchor-links h6";
      var headers = document.querySelectorAll(className);

      for (var i = 0; i < headers.length; i++) {
        var header = headers[i];
        if (typeof header.id !== "undefined" && header.id !== "") {
          header.prepend(anchorForId(header.id));
        }
      }
    }
  };
</script>
